<?Php

class mdata extends CI_Model {

   
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
	
	function get_menu($par1="",$par2=""){
		$sql = "select id_menu_frontend, menu_name, stat, url, group_na, have_sub_menu from m_menu_frontend where stat = 'A' and group_na = 'F'";
		$res = $this->db->query($sql)->result_array();
		return $res;
	}
	
	function get_sub_menu($par1="",$par2=""){
		$sql = "select sub_menu_name, url, stat, id_menu from m_sub_menu_frontend where stat = 'A'";
		$res = $this->db->query($sql)->result_array();
		return $res;
	}
	
	function get_cat($par1="",$par2=""){
		$sql = "select id, kategori from cl_kategori";
		$res = $this ->db->query($sql)->result_array();
		return $res;
	}
	
	function get_data($par1="",$par2="",$p3="",$p4="",$p5=""){
		$where =' WHERE 1=1 ';
		switch ($par1){
			case "tbl_produk_user":
			$where = "AND 1=1";
			$limit="";
			//if($p3 !="" && $p4 !="")
			//echo $p3;
			//$limit ="limit $p3, $p4";
				if($this->input->post("id")!=''){
					$str=explode("_",$this->input->post("id"));
					if($str[0]=='kat'){
						$where .=' AND A.cl_kategori_id='.$str[1];
					}
				}
				$key=$this->input->post('keyword');
				$kat=$this->input->post('cl_kategori');
				$harga_awal=$this->input->post('harga_awal');
				$harga_akhir=$this->input->post('harga_akhir');
				if($key){
					$where .=" AND A.nama_produk like '%$key%' OR A.deskripsi like '%$key%' OR E.nama_toko like '%$key%' OR E.deskripsi like '%$key%'";
				}
				
				if($kat){
					$where .=' AND A.cl_kategori_id='.$kat;
				}
				
				if($harga_awal){
					$where .=' AND A.cl_kategori_id='.$kat;
				}
				
				
				
					
				if($par2=='filter_toko'){
					if($p3==NULL){
						$p3=0;
					}
					$limit .=' limit '.$p3.','.$p4.'';
				}elseif($par2=='filter_agen'){
					if($p4==NULL){
						$p4=0;
					}
					$where .=' AND A.tbl_toko_user_id='.$p3;
					$limit .=' limit '.$p4.','.$p5.'';
				}elseif($par2=='filter_top'){
					if($p3==NULL){
						$p3=0;
					}
					$limit .=' limit 0,4';
				}
				
				
				
				
				$sql="SELECT A.id,A.nama_produk,A.deskripsi,format(A.harga,2)as harga_na,A.harga,A.status_aktif,A.persetujuan,A.tbl_user_id,
						B.nama_lengkap,E.nama_toko,A.cl_warna_id,D.warna,A.cl_kategori_id,C.kategori,A.tgl_buat,
						A.foto_1,A.foto_2,A.foto_3,A.rating
						FROM tbl_produk_user AS A
						INNER JOIN tbl_user AS B ON A.tbl_user_id = B.user_id
						INNER JOIN cl_kategori AS C ON A.cl_kategori_id = C.id
						INNER JOIN cl_warna AS D ON A.cl_warna_id = D.id
						INNER JOIN tbl_toko_user E on A.tbl_toko_user_id=E.id ".$where . " AND A.status_aktif=1 order by A.id DESC
						".$limit."";
					//echo $sql;
					switch($par2){
						case "":
						break;
						case "total":
							return $this->db->query($sql)->num_rows();
						break;
						default:
							return $this->db->query($sql)->result_array();
						break;
					}
						
				//$this->db->limit($par1,$p3);
				/*if($par2!=""){
					return $this->db->query($sql)->result_array();
				}else if($par2=="total"){
					return $this->db->query($sql)->num_rows();
					}*/
			break;
			
			case "jml":
				$res=$this->db->get_where("tbl_produk_user")->result_array();
				return count($res);
			break;
			
			case "iklan":
				$sql="SELECT * FROM t_iklan where menu_id=".$par2;
			break;
			
			case "allproduct":
				$sql="	select a.rating,a.nama_produk,b.nama_toko,c.nama_lengkap,a.id,a.deskripsi,a.harga,a.foto_1,a.foto_2,a.foto_3 
						from tbl_produk_user as a 
						left join tbl_toko_user as b 
						on a.tbl_toko_user_id = b.id
						left join tbl_user as c 
						on a.tbl_user_id = c.user_id
						";
				$row = $this->db->query($sql)->result_array();
				return $row;
			break;
			
			case "tbl_user":
				$sql="SELECT * FROM tbl_user where user_id='".$par2."' and status_user='1'";
				$row = $this->db->query($sql)->result_array();
				return $row;	
			break;
			
			case "highlight_product":
				$sql="SELECT A.*,B.nama_toko,B.logo,C.nama_lengkap,C.tgl_buat FROM tbl_produk_user A
					LEFT JOIN tbl_toko_user B ON A.tbl_toko_user_id=B.id
					LEFT JOIN tbl_user C ON A.tbl_user_id=C.user_id WHERE A.id='".$par2."'";
				$row = $this->db->query($sql)->row();
				return $row;
			break;
			
			case "portofolio":
				if($par2=="paging"){
				$limit="";
				if($p3==NULL){
					$p3 = 0;
				}
					$limit .=' limit '. $p3.','.$p4;
				//echo $limit;
				//exit;
			}
				$sql="SELECT * from tbl_porto_folio".$limit;
				$row = $this->db->query($sql)->result_array();
				return $row;
			break;
			
			case "narsis":
			if($par2=="paging"){
			$limit="";
				if($p3==NULL){
					$p3 = 0;
				}
					$limit .=' limit '. $p3.','.$p4;
				//echo $limit;
				//exit;
			}
				$sql="SELECT * from tbl_narsis".$limit;
				$row = $this->db->query($sql)->result_array();
				return $row;
			break;
			
			case "faq":
				$sql="SELECT * from tbl_faq WHERE status = 'show'";
				$row = $this->db->query($sql)->result_array();
				return $row;
			break;
			
			case "tbl_toko_user":
				$sql="SELECT A.* from tbl_toko_user A 
						LEFT JOIN tbl_user B ON A.tbl_user_id=B.user_id
						WHERE B.status_user = 1";
				$row = $this->db->query($sql)->result_array();
				return $row;
			break;
			case "cl_warna":
				$sql="SELECT A.* from cl_warna A ";
				$row = $this->db->query($sql)->result_array();
				return $row;
			break;
			
			case "product_agent":
				$sql="SELECT * from tbl_produk_user WHERE tbl_toko_user_id = '$par2'";
				$row = $this->db->query($sql)->result_array();
				return $row;
			break;
			
			case "agen":
			if($par2=="paging"){
			$limit="";
				if($p3==NULL){
					$p3 = 0;
				}
					$limit .=' limit '. $p3.','.$p4;
				//echo $limit;
				//exit;
			}
				$sql2=	"SELECT tbl_toko_user.id AS ID_TOKO, tbl_toko_user.logo AS LOGO, tbl_toko_user.nama_toko AS TOKO FROM tbl_toko_user ".$limit;
				$row = $this->db->query($sql2)->result_array();
				return $row;
			break;
			
			case "count_product":
				$sql="SELECT
a.id,
a.tbl_toko_user_id,
a.tbl_user_id,
a.nama_produk,
a.deskripsi,
a.harga,
a.status_aktif,
a.persetujuan,
a.cl_warna_id,
a.cl_kategori_id,
a.tgl_buat,
a.update_date,
a.foto_1,
a.foto_2,
a.foto_3,
a.rating,
b.id
FROM
tbl_produk_user AS a ,
tbl_toko_user AS b
WHERE
a.tbl_toko_user_id = b.id

";
	$row2 = $this->db->query($sql)->result_array();
	return $row2;
			break;
			
			case "kirim_email":
				$sql="SELECT
						pe.id,
						pe.tbl_produk_id,
						pe.nama,
						pe.alamat,
						pe.telp,
						pe.hp,
						pe.email,
						pe.jml,
						pe.tgl_buat,
						pe.design,
						pe.`comment`,
						pu.nama_produk,
						pu.foto_1,
						A.nama_toko,
						B.nama_lengkap
							FROM
							tbl_pesan_email AS pe
							LEFT JOIN tbl_produk_user AS pu ON pe.tbl_produk_id = pu.id
							LEFT JOIN tbl_toko_user AS A ON pu.tbl_toko_user_id=A.id
							LEFT JOIN tbl_user B ON A.tbl_user_id=B.user_id
							WHERE pe.tbl_produk_id = ".$par2;
				$row = $this->db->query($sql)->row();
				return $row;
			break;
		
		}
		//$res=$this->db->query($sql)->result_array();
		return $this->get_jsonna($sql);
		//return $res;
		
	}
	
	function get_jsonna($sql){
		$page = (integer) (($this->input->post('page')) ? $this->input->post('page') : "1");
		$limit = (integer) (($this->input->post('rows')) ? $this->input->post('rows') : "15");
		 
		$count = $this->db->query($sql)->num_rows();
		
		if( $count >0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
		if ($page > $total_pages) $page=$total_pages; 
		$start = $limit*$page - $limit; // do not put $limit*($page - 1)
		if($start<0) $start=0;
		  
		$sql = $sql . " LIMIT $start,$limit";
		
		 $data=$this->db->query($sql)->result();  
			if($total_pages==0){
				$kos='{"total":0,"rows":[]}';
				return $kos;
			}
			else{  	
				if($data){
				   $responce->rows= $data;
				   $responce->total =$count;
				   return json_encode($responce);
				}else{ 
				   return "";
				}   
			}
 	}
	
	function isi_combo($table,$field_id,$field_display){
		$sql="select ".$field_id." as id ,".$field_display." as text from ".$table;
		return $this->db->query($sql)->result_array();
	}
	
	function input_pertanyaan($par1="",$par2=""){
		$pertanyaan = $this->input->post('pertanyaan');
		//echo $pertanyaan;
		$sql = "insert into tbl_faq (id_faq,pertanyaan,status) values ('','$pertanyaan','hide')";
		$row = $this->db->query($sql);
	}
	
	function nama_toko($par1="",$par2=""){
		//echo $par1;
		$sql = "select * from tbl_toko_user where id = '$par1'";
		$row = $this->db->query($sql)->result_array();
		return $row;
	}
	
	function simpan($p1="",$post=""){
		$this->db->trans_begin();
		$id=$this->input->post("id");
		$sts=$post["sts"];
		unset ($post["sts"]);
		switch ($p1){
			case "tbl_faq":
				if($sts=='edit'){
					$this->db->where('id',$id);	
				}
			break;
		}
		
		if($sts=='add'){
			$post["tgl_buat"]=date('Y-m-d');
			$this->db->insert($p1,$post);
		}
		
		else{
			$post["update_date"]=date('Y-m-d');
			$this->db->update($p1,$post);
		}
		
		if($this->db->trans_status() == false){
		$this->db->trans_rollback();
		return "Data not saved";
		} else{
		return $this->db->trans_commit();
		}
		
		
	}
	
	
}